﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;

namespace patchPack
{
	/// <summary>
	/// MainWindow.xaml 的交互逻辑
	/// </summary>
	public partial class MainWindow : Window
	{
		public static Dictionary<string, string> fileInfoTable = new Dictionary<string, string>();
		public static Dictionary<string, string> scriptInfoTable = new Dictionary<string, string>();

		public MainWindow()
		{
			InitializeComponent();
		}

		private void IterListener(string basePath, string packFileName, string orgFilePath)
		{
			if (!packFileName.EndsWith("lua"))
			{
				fileInfoTable[packFileName] = orgFilePath;
				LogicMod.CopyAssetsToPatch(basePath, packFileName, orgFilePath);
			}
			else
			{
				string hashName = HashMod.CreateMD5Digest(packFileName);
				string secName = AESMod.AESEncrypt(packFileName);
				scriptInfoTable[hashName] = secName;

				string content = FileHandle.ReadFile(basePath + "/" + orgFilePath);
				string secContent = AESMod.AESEncrypt(content);
				LogicMod.CopyScriptsToPatch(basePath, hashName, secContent);
			}
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			string dirName = FileHandle.OpenDir();
			dirNameText.Text = dirName;

			if(dirName == "")
			{
				return;
			};

			string parentPath = StringMod.GetParentPath(dirName);
			parentDirText.Text = parentPath;

			LogicMod.CreatePatchDir(parentPath);

			FileHandle.ListFiles(dirName, "", "", IterListener);

			Dictionary<string, Dictionary<string, string>> tInfo = new Dictionary<string, Dictionary<string, string>>();
			tInfo["tAssets"] = fileInfoTable;
			tInfo["scripts"] = scriptInfoTable;
			string jsonHashCode = JsonConvert.SerializeObject(tInfo);
			LogicMod.WriteContentToInfoFile(parentPath, jsonHashCode);
			MessageBox.Show("complete");
		}
	}
}
